﻿#region Using directives

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Weazel.Toad.TextEditor;
using Weazel.Toad.TextEditor.Document;

#endregion

namespace Weazel.Toad.Application.Items
{
  [Serializable]
  public class TextFile : ItemBase
  {
    public override string FileTypeName
    {
      get { return "Text"; }
    }

    public override string Extension
    {
      get { return "txt"; }
    }

    public string Contents
    {
      get
      {
        System.Diagnostics.Debug.Assert(tabPage != null);
        return (this.tabPage.Controls[0] as TextEditorControl).Document.TextContent;
      }
    }

    public Document Document
    {
      get
      {
        return (this.tabPage.Controls[0] as TextEditorControl).Document;
      }
    }

    public TextEditorControl TextEditor
    {
      get
      {
        return this.tabPage.Controls[0] as TextEditorControl;
      }
    }

    public TextFile()
    {
      this.DisplayName = "unnamed.txt";
    }

    #region protected methods

    protected override void OpenTabPageInternal()
    {
      TextEditorControl textEditorControl = new TextEditorControl();
      textEditorControl.ShowInvalidLines = false;
      textEditorControl.ShowSpaces = false;
      textEditorControl.ShowEOLMarkers = false;
      textEditorControl.ShowTabs = false;

      textEditorControl.Dock = System.Windows.Forms.DockStyle.Fill;
      textEditorControl.Document.Highlighter = HighlighterFactory.CreateHighlightingStrategyForFile(this.DisplayName);
      
      // de-registered in CloseInternal() !!!
      textEditorControl.Document.DocumentChanged += new DocumentEventHandler(Document_DocumentChanged);

      this.tabPage.Controls.Add(textEditorControl);      
    }


    protected override void SaveInternal()
    {
      System.Diagnostics.Debug.Assert(Filename != null);
      System.Diagnostics.Debug.Assert(tabPage != null);

      Document doc = (tabPage.Controls[0] as TextEditorControl).Document;
      using (StreamWriter writer = new StreamWriter(this.Filename))
      {
        writer.Write(doc.TextContent);
      }
    }

    protected override void OpenInternal()
    {
      System.Diagnostics.Debug.Assert(Filename != null);
      System.Diagnostics.Debug.Assert(tabPage != null);

      Document doc = (tabPage.Controls[0] as TextEditorControl).Document;
      using (StreamReader reader = new StreamReader(this.Filename))
      {
        doc.TextContent = reader.ReadToEnd();
      }
    }

    protected override void CloseInternal()
    {
      Document.DocumentChanged -= new DocumentEventHandler(Document_DocumentChanged);
    }


    #endregion

  
    void  Document_DocumentChanged(object sender, DocumentEventArgs e)
    {
      OnItemContentsChanged();
    }

    public virtual void GotoLine(int lineNumber)
    {
      TextEditor.TextAreaControl.Caret.Line = lineNumber;
      
      // make sure selected line is visible
      TextEditor.TextAreaControl.ScrollToCaret();

      // make sure text editor regains focus
      TextEditor.Focus(); 
    }

		public virtual void GotoPosition(int lineNumber, int columnNumber)
    {
      TextEditor.TextAreaControl.JumpTo(lineNumber, columnNumber);			
    }
  }
}
